var storage = window.localStorage;

function setUserRole(userRole) {
    storage.removeItem("userRole");
    storage.setItem("userRole", JSON.stringify(userRole));
};

function cleanStorage() {
    storage.clear();
};

function setStorage(key, value) {
    storage.removeItem(key);
    storage.setItem(key, JSON.stringify(value));
}

function getStorage(key) {
    var val = JSON.parse(storage.getItem(key));
    return val == null ? '' : val;
}

function removeStorage(key) {
    storage.removeItem(key);
}
function getAndRemoveStorage(key) {
    var val  = getStorage(key);
    removeStorage(key);
    return val;
}

function setUserName(userName) {
    storage.removeItem("userName");
    storage.setItem("userName", JSON.stringify(userName));
};

function setTokenId(tokenId) {
    storage.removeItem("tokenId");
    storage.setItem("tokenId", JSON.stringify(tokenId));
};


function setUserId(userId) {
    storage.removeItem("userId");
    storage.setItem("userId", JSON.stringify(userId));
};

function setUserAccount(userAccount) {
    storage.removeItem("userAccount");
    storage.setItem("userAccount", JSON.stringify(userAccount));
};

function getUserAccount() {
    var val = JSON.parse(storage.getItem("userAccount"));
    return val == null ? '' : val;
};

function getUserName() {
    var val = JSON.parse(storage.getItem("userName"));
    return val == null ? '' : val;
};

function getUserRole() {
    var val = JSON.parse(storage.getItem("userRole"));
    return val == null ? '' : val;
};

function getTokenId() {
    var val = JSON.parse(storage.getItem("tokenId"));
    return val == null ? '' : val;
};

function getUserId() {
    var val = JSON.parse(storage.getItem("userId"));
    return val == null ? '' : val;
};

function getContextPath() {
    var base = document.getElementsByTagName('base')[0];
    if (base && base.href && (base.href.length > 0)) {
        base = base.href;
    } else {
        base = document.URL;
    }
    ;
    return base;
};

function reload(href) {
    var base = getContextPath();
    var url = base.substr(base.indexOf("#"), base.length);
    if (url == href) {
        window.location.href = href + "0";
    } else {
        window.location.href = href;
    }
    ;
};

function getTrunkName(projectName) {
    if (projectName.indexOf("_") != -1) {
        return projectName.split("_")[0];
    } else {
        return projectName;
    }
};

function getUCloudAddress(ip) {
    return 'http://' + ip + ':5000/api/v1';
};

function getEditionPath(projectName, editionNum) {
    return "/usr/local/Beetle/projects/" + getTrunkName(projectName)
        + "/" + projectName + "/" + editionNum;
};

function bytesToSize(size) {
    if (size.indexOf('kB') != -1) {
        return (size.replace('kB', '') / (1024 * 1024)).toPrecision(3) + "G";
    } else if (size.indexOf('MB') != -1) {
        return (size.replace('MB', '') / 1024).toPrecision(3) + "G";
    }
};

function parse(date) {//20161211-120210
    var year = date.substring(0, 4);
    var month = date.substring(4, 6) - 1;
    var day = date.substring(6, 8);
    var hour = date.substring(9, 11);
    var min = date.substring(11, 13);
    var second = date.substring(13);
    return new Date(year, month, day, hour, min, second);
};

function arrayContains(array, obj) {
    if (array.length == 0) {
        return false;
    }
    for (var i = 0; i < array.length; i++) {
        if (array[i] === obj) {
            return true;
        }
    }
    return false;
};

//ucloud是不是集成环境
function ucIsIntegration(ip) {
    return arrayContains(integration_agent_list, ip);
};

/**
 * 通过角色判断用户是否可操作
 * 当某一个功能(按钮、图标等)需要根据用户角色判断时，调用
 * 默认admin可以操作所有按钮
 * 例：页面中有一个按钮只有ops与pm可操作，则调用 isUserCanOperateByRole('ops','pm') 若返回true  说明当前用户拥有ops或pm或admin权限  可以操作
 */
function isUserCanOperateByRole() {
    var userRole = getUserRole();
    if (arrayContains(userRole, 'admin')) {
        return true;
    }
    if (arguments.length == 0) {
        return false;
    }
    for (var i = 0; i < arguments.length; i++) {
        if (arrayContains(userRole, arguments[i])) {
            return true;
        }
    }
    return false;
};